<?php

namespace App\Http\Controllers\Api\Message;

use App\Http\Requests\Api\Message\MessageHideWindowRequest;
use App\Http\Requests\Api\Message\MessageReadRequest;
use App\Http\Requests\Api\Message\MessageRecordRequest;
use App\Http\Requests\Api\Message\MessageWindowRequest;
use App\Http\Resources\RecordResource;
use App\Http\Resources\StrangerResource;
use App\Http\Resources\UserResource;
use App\Models\Message;
use App\Models\Record;
use App\Models\Stranger;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use App\Services\OSS;


class MessageController extends Controller
{
    //
    public function list(Request $request)
    {
        $user = Auth::guard('api')->user();
        return StrangerResource::collection(
            Stranger::where('user_id',$user->id)->where('hide',0)->get()
        );
    }
    public function msgboxJson(Request $request)
    {
        $user = Auth::guard('api')->user();
        $msgList = null;
        $msg = Message::where('uid', $user->id)->orderBy('updated_at','dasc')->get();
        if (!$msg->isEmpty()) {
            foreach ($msg as $key => $v) {
                $v['user'] = User::find($v->from);
                $v['time'] = $v->updated_at->format('Y-m-d H:i:s');
                $msgList[$key] = $v;
            }
        }
        return response()->json([
            'code' => 0,
            'message' => '',
            'data' => $msgList
        ]);
    }

    public function msgboxRead(Request $request)
    {
        $user = Auth::guard('api')->user();
        return (Message::where('uid',$user->id)->where('read',0)->update(['read' => 1]));
    }
    public function msgboxNoread(Request $request)
    {
        return Message::where('uid',$request->user()->id)->where('read',0)->count();
    }

    public function record(MessageRecordRequest $request)
    {
        $user = Auth::guard('api')->user();
        $window_id = $request->input("id");
        $records = Record::where([
            ['from_id', '=', $window_id],
            ['to_id', '=', $user->id],
        ])->orwhere([
            ['from_id', '=', $user->id],
            ['to_id', '=', $window_id],
        ])->latest()->paginate(config('api.pagination.record'));
        return RecordResource::collection($records);
    }

    public function createWindow(MessageWindowRequest $request)
    {
        $user = Auth::guard('api')->user();
        if(
            !Stranger::where(
                "user_id",$user->id
            )->where(
                "stranger_id",$request->id
            )->exists()){
            Stranger::create([
                "user_id" => $user->id,
                "stranger_id" => $request->id,
            ]);
        }
        if(
            !Stranger::where(
                "user_id",$request->id
            )->where(
                "stranger_id",$user->id
            )->exists()){
            Stranger::create([
                "user_id" => $request->id,
                "stranger_id" => $user->id,
            ]);
        }
    }

    public function hideWindow(MessageHideWindowRequest $request)
    {
        $user = Auth::guard('api')->user();
        $window = Stranger::find($request->id);
        if($window->user_id != $user->id){
            abort(403,'无权限');
        }else{
            $window->hide = 1;
            return response()->json([
                'code' => 0,
                'message' => '删除会话成功响应',
            ]);
        }
    }

}
